package cn.zd.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import cn.zd.demo1.adapter.MyGridViewBaseAdapter;
import cn.zd.demo1.adapter.MyListViewBaseAdapter;
import cn.zd.demo1.util.DisplayUtil;

public class GridViewActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, View.OnClickListener {

    private MyGridViewBaseAdapter adapter;
    private GridView gv_1;
    private int col_count = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Toast.makeText(this,this.getClass().getName(),Toast.LENGTH_LONG).show();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridview);
        gv_1 = findViewById(R.id.gv_1);
        adapter = new MyGridViewBaseAdapter(this);
        gv_1.setAdapter(adapter);
        gv_1.setOnItemClickListener(this);
        findViewById(R.id.btn_col).setOnClickListener(this);
        findViewById(R.id.btn_stretch_mode).setOnClickListener(this);
        findViewById(R.id.btn_default).setOnClickListener(this);
        findViewById(R.id.btn_uniform).setOnClickListener(this);
        findViewById(R.id.btn_stm_sp).setOnClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this,"点击了"+adapter.getData(position).getName(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_col) {
            gv_1.setNumColumns(gv_1.getNumColumns() == 2?3:2);
        } else if(v.getId() == R.id.btn_stretch_mode) {
            Toast.makeText(this,"stretchMode==null",Toast.LENGTH_SHORT).show();
            gv_1.setColumnWidth(DisplayUtil.dp2px(this,100));
            gv_1.setStretchMode(GridView.NO_STRETCH);
        } else if(v.getId() == R.id.btn_default) {
            Toast.makeText(this,"stretchMode==STRETCH_COLUMN_WIDTH",Toast.LENGTH_SHORT).show();
            gv_1.setColumnWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            gv_1.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        } else if(v.getId() == R.id.btn_uniform) {
            Toast.makeText(this,"stretchMode==STRETCH_SPACING_UNIFORM",Toast.LENGTH_SHORT).show();
            gv_1.setColumnWidth(DisplayUtil.dp2px(this,100));
            gv_1.setStretchMode(GridView.STRETCH_SPACING_UNIFORM);
        } else if(v.getId() == R.id.btn_stm_sp) {
            Toast.makeText(this,"stretchMode==STRETCH_SPACING_UNIFORM",Toast.LENGTH_SHORT).show();
            gv_1.setColumnWidth(DisplayUtil.dp2px(this,100));
            gv_1.setStretchMode(GridView.STRETCH_SPACING);
        }
    }
}